/*
Copyright © 2023 taiping
*/

package cmd

import (
	"fmt"
	"os"
	"text/template"
	"wit-gen/config"
	"wit-gen/templates"
)

type GenType struct {
	M1 string
	M2 string
	M3 string
}

func GenTemplate(args []string) {
	data := GenType{
		M1: args[0],
		M2: args[1],
		M3: args[2],
	}
	fmt.Println("模块创建中...")
	fmt.Println(data)

	fmt.Println("解析模板中......")

	controller, err := template.New("controller").Parse(templates.Controller)
	if err != nil {
		panic(err)
	}
	service, err := template.New("service").Parse(templates.Service)
	if err != nil {
		panic(err)
	}
	router, err := template.New("router").Parse(templates.Router)
	if err != nil {
		panic(err)
	}
	//输出controller文件
	outControllerFile := config.ControllerPath + data.M1 + ".go"
	controllerF, err := os.OpenFile(outControllerFile, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		panic(err)
	}
	err = controller.Execute(controllerF, data)
	if err != nil {
		panic(err)
	}
	fmt.Println("=========>生成controller成功！")
	//输出service文件
	outServiceFile := config.ServicePath + data.M1 + ".go"
	serviceF, err := os.OpenFile(outServiceFile, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		panic(err)
	}
	err = service.Execute(serviceF, data)
	if err != nil {
		panic(err)
	}
	fmt.Println("=========>生成service成功！")
	//输出service文件
	outRouterFile := config.RouterPath + data.M1 + ".go"
	routerF, err := os.OpenFile(outRouterFile, os.O_CREATE|os.O_WRONLY, 0755)
	if err != nil {
		panic(err)
	}
	err = router.Execute(routerF, data)
	if err != nil {
		panic(err)
	}
	fmt.Println("=========>生成router成功！")
}
